/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2016        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */
#include "ff_port.h"
#include "loger.h"

#define TAG "DISKIO"

/* Definitions of physical drive number for each drive */
#define DEV_RAM		0	/* Example: Map Ramdisk to physical drive 0 */
#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */

enum
{
    FF_DEV_SPIFLASH = 0,
    FF_DEV_SDCARD,
    FF_DEV_USB,
    FF_DEV_MAX,
};

extern const _ff_port_opt_t ff_port_fls;
extern const _ff_port_opt_t ff_port_sd;
#if (CONFIG_HAL_USB_HOST_CLASS_MSC_ENABLE)
extern const _ff_port_opt_t ff_port_udisk;
#endif

static const _ff_port_opt_t *ff_opt[FF_VOLUMES] = 
{
    [FF_DEV_SPIFLASH] = (_ff_port_opt_t*)&ff_port_fls,
    [FF_DEV_SDCARD] = (_ff_port_opt_t*)&ff_port_sd,

#if (CONFIG_HAL_USB_HOST_CLASS_MSC_ENABLE)
    [FF_DEV_USB] = (_ff_port_opt_t*)&ff_port_udisk,
#else
    [FF_DEV_USB] = NULL,
#endif
};


int disk_get_vol_by_name(const char *name)
{
    int i;
    
    if (name == NULL) return -1;

    for (i = 0; i < FF_VOLUMES; i++)
    {
        if (ff_opt[i] != NULL)
        {
            if (strcmp(ff_opt[i]->name, name) == 0)
            {
                return i;
            }
        }
    }
    
    return -1;
}


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat;

    if (pdrv >= FF_VOLUMES) return RES_ERROR;
    if (ff_opt[pdrv] == NULL) return STA_NODISK;

    stat = ff_opt[pdrv]->status();

	return stat;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize(
    BYTE pdrv /* Physical drive nmuber to identify the drive */
)
{
    DSTATUS stat;

    if (pdrv >= FF_VOLUMES) return RES_ERROR;
    if (ff_opt[pdrv] == NULL) return STA_NODISK;

    stat = ff_opt[pdrv]->init();
    // LOG_I(TAG, "init(%d) = %d", pdrv, stat);

    return stat;
}

/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read(
    BYTE pdrv,    /* Physical drive nmuber to identify the drive */
    BYTE *buff,   /* Data buffer to store read data */
    DWORD sector, /* Start sector in LBA */
    UINT count    /* Number of sectors to read */
)
{
    DRESULT res;

    if (pdrv >= FF_VOLUMES) return RES_ERROR;
    if (ff_opt[pdrv] == NULL) return RES_PARERR;

    res = ff_opt[pdrv]->read(buff, sector, count);
    // LOG_I(TAG, "read(%d) = %d", pdrv, res);

    return res;
}

/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write(
    BYTE pdrv,        /* Physical drive nmuber to identify the drive */
    const BYTE *buff, /* Data to be written */
    DWORD sector,     /* Start sector in LBA */
    UINT count        /* Number of sectors to write */
)
{
    DRESULT res;

    if (pdrv >= FF_VOLUMES) return RES_ERROR;
    if (ff_opt[pdrv] == NULL) return RES_PARERR;

    res = ff_opt[pdrv]->write(buff, sector, count);
    // LOG_I(TAG, "write(%d) = %d", pdrv, res);

    return res;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl(
    BYTE pdrv, /* Physical drive nmuber (0..) */
    BYTE cmd,  /* Control code */
    void *buff /* Buffer to send/receive control data */
)
{
    DRESULT res;

    if (pdrv >= FF_VOLUMES) return RES_ERROR;
    if (ff_opt[pdrv] == NULL) return RES_PARERR;

    res = ff_opt[pdrv]->ioctl(cmd, buff);

    return res;
}


